<?php
/**
 * 单条分享审核
 * 
 * @copyright dongping
 * @since 2012-03-25
 */


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	//check refer  -  add by xinwei
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	
	// 分享ID
	cgi::both($g_cgival["status_id"], "status_id", 0);
	$g_cgival["status_id"] = intval($g_cgival["status_id"]);
	

	// 设置分享状态
	cgi::both($g_cgival["status"], "status", 0);
	if ($g_cgival["status"] != 0 and $g_cgival["status"] != 1)
	{
		$g_cgival["status"] = 0;
	}
	
	if (isset($_POST["status_ids"]) and count($_POST["status_ids"]) > 0)
	{
		foreach($_POST["status_ids"] as $k => $v)
		{
			$g_cgival["status_ids"][] = intval($v);
		}
	}
	if (isset($_POST["status_id"]))
	{
		$_POST["status_id"] = intval($_POST["status_id"]);
		$g_cgival["status_ids"][] = $_POST["status_id"];
	}
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$status = $g_cgival["status"];
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();	// 拿到当前站点的site_id
	$admin_id = admin::admin_get_login_uid();	// 拿到当前管理员ID
	$admin_name = admin::admin_get_login_user_name();	// 拿到当前管理员用户名
	$mod_food_status_comment = new mod_food_status_comment();
	
	$admin_log = new admin_log();
	$db = new db_food_status();
	$db_tag = new db_food_status_tag();
//  	$db->debug = true;
	if (is_array($g_cgival["status_ids"]))
	{
		// 根据分享ids获取创建人信息 add by zhanghuan at 2012/06/07
		$status_list = $status_uids = array();
		$status_list = $db->get_list_by_status_ids($g_cgival["status_ids"]);
		foreach ($status_list as $k => $v)
		{
			$status_uids[$k] = $v["create_uid"];
		}
		
		$ret = $db->admin_update_batch_status_by_status_id($site_id, $g_cgival["status_ids"], $status, $admin_id, $admin_name);
		
		/**
		 * 根据status_id 获取res_id,如果res_id>0, 更新tag的is_rstrt
		 * add by zhanghuan at 2012/05/21
		 */
		$db_tag->admin_update_batch_rstrt_by_status_id($site_id, $g_cgival["status_ids"], $status, $admin_id, $admin_name);
		
		if($status == 1)
		{
			//更新status_id对应的评论 edit by yangchao 20120523
			$mod_food_status_comment->update_comment_by_status_ids($g_cgival["status_ids"]);
		}
		elseif ($status == 0)
		{
			/**
			 * 删除动态信息
			 * modified by zhanghuan at 2012/06/08 
			 */
			$mod_food_user_feed = new mod_food_user_feed();
			foreach ($g_cgival["status_ids"] as $status_id)
			{
				$mod_food_user_feed -> del_food_user_feed_by_class($status_id,'183');
			}
			/* 取专辑内的微博 */
			$db_food_album_status = new db_food_album_status();
			$db_food_album = new db_food_album();
			$album_status_list = $db_food_album_status->admin_get_album_status_list_by_status_id($g_cgival["status_ids"]);

			/* 删除专辑内的微博 */
			$db_food_album_status->admin_delete_by_status_id($g_cgival["status_ids"]);

			/* 取专辑ID */
			foreach ($album_status_list as $val)
			{
				$album_ids[] = $val['album_id'];

				/* 取专辑微博总数 */
				$album_status_count = $db_food_album_status->admin_get_album_status_count_by_album_id($val['album_id'],true);

				/* 更新专辑微博总数 */
				$db_food_album->admin_update_food_album_status_count_by_id($album_status_count,$admin_id,$val['album_id']);
				
			}
		}
		
		// 更新用户的分享数 add by zhanghuan at 2012/06/07
		$mod_food_user = new mod_food_user();
		$mod_food_user->update_status_count_by_uids(array_unique($status_uids));
		
	}
	
	$g_show["ret"] = $ret;
	
	$status_ids_str = implode(',', $g_cgival["status_ids"]);
	if ($status == 1)
	{
		$admin_log->admin_insert_log_info($admin_id, $site_id, 2, "审核通过 美食分享 status_id={$status_ids_str}");
	}
	else 
	{
		$admin_log->admin_insert_log_info($admin_id, $site_id, 2, "驳回 美食分享 status_id={$status_ids_str}");
	}
}




try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album/ajax_album_thumb_rec.php");
	
	check_cgi_pro();
	get_data();
	
	$g_show["error"] = 0;
	$g_show["errmsg"] = '';	
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	
	
	echo json_encode($g_show);
}

